﻿//--------------------------------------------------------------------------
// 
//  Copyright (c) Chili Software.  All rights reserved. 
// 
//  File: OPathException.cs
//
//  Description: Thrown if an OPath can't be parsed.
// 
//--------------------------------------------------------------------------

using System;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Chili.Opf3.Query.OPathParser
{

	/// <summary>
	/// Exception is thrown if an OPath expression is invalid.
	/// </summary>
	/// <remarks>
	/// This exception is thrown if an OPath expression is invalid and could not be parsed successfully.
	/// </remarks>
	[Serializable]
	public sealed class OPathException : Exception
	{
		private string _token;
		private string _oPath;

		/// <summary>
		/// Creates a new instance of the
		/// <see cref="OPathException">OPathException Class</see>.
		/// </summary>
		/// <param name="message">A message that describes the current exception.</param>
		/// <param name="token">The current token which generated the exception.</param>
		/// <param name="oPath">The whole OPath expression that is parsed.</param>
		public OPathException(string message, string token, string oPath) : base(message)
		{
			_token = token;
			_oPath = oPath;
		}

		/// <summary>
		/// Creates a new instance of the
		/// <see cref="OPathException">OPathException Class</see>.
		/// </summary>
		/// <param name="message">A message that describes the current exception.</param>
		/// <param name="inner">The exception that caused the current exception.</param>
		/// <param name="token">The current token which generated the exception.</param>
		/// <param name="oPath">The whole OPath expression that is parsed.</param>
		public OPathException(string message, string token, string oPath, Exception inner) : base(message, inner)
		{
			_token = token;
			_oPath = oPath;
		}

		/// <summary>
		/// Creates a new instance of the
		/// <see cref="OPathException">OPathException Class</see>.
		/// </summary>
		private OPathException(SerializationInfo info, StreamingContext context) : base(info, context)
		{
			_token = (string)info.GetValue("token", typeof(string));
			_oPath = (string)info.GetValue("oPath", typeof(string));
		}

		/// <summary>
		/// Restores the objects data after serialization.
		/// </summary>
		[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);
			info.AddValue("token", Token);
			info.AddValue("oPath", OPath);
		}

		/// <summary>
		/// Returns the token that generated the exception.
		/// </summary>
		/// <value>The token that generated the exception.</value>
		public string Token
		{
			get { return _token; }
		}

		/// <summary>
		/// Returns the whole OPath expression that is parsed.
		/// </summary>
		/// <value>The OPath expression that is parsed.</value>
		public string OPath
		{
			get { return _oPath; }
		}

		/// <summary>
		/// Returns a string representation of the current instance of the class.
		/// </summary>
		public override string ToString()
		{
			return string.Format("Parsing error in Token: '{1}' and OPath: '{2}'. {0}", Message, _token, _oPath);
		}
	}
}
